﻿using HookupAppForm.Dto;
using HookupAppForm.MyUserControl;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormHDC : Form
    {
        private readonly string url = "api/services/app/HKP_HDC/";
        private APIClient Client { get; set; }
        private HKP_HDCDto Item { get; set; }
        public FormHDC(HKP_HDCDto _item, APIClient _client)
        {
            InitializeComponent();
            Item = _item;
            Client = _client;
        }

        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            string act = Item == null ? "Create" : "Update";
            string result = string.Empty;
            if (Item != null) {
                Item.ToolId = Guid.Parse(ToolIdBox.SelectedValue?.ToString());
                Item.Bay = txtBay.Text.Trim();
                Item.Building = building.selectedText;
                Item.FabArea = txtFabArea.Text.Trim();
                Item.Floor = floor.selectedText;
                Item.SubmainNo = txtSubmainNo.Text.Trim();
                Item.SysNo = SysNoBox.SelectedValue?.ToString();
                Item.TakeoffNo = txtTakeoffNo.Text.Trim();
                Item.X = numX.Value;
                Item.Y = numY.Value;
                Item.Z = numZ.Value;
                string jsons = JsonConvert.SerializeObject(Item);
                result = Client.Put(url + act, jsons);
            }
            else
            {
                CreateHKP_HDCDto dto = new CreateHKP_HDCDto();
                dto.ToolId = Guid.Parse(ToolIdBox.SelectedValue?.ToString());
                dto.Bay = txtBay.Text.Trim();
                dto.Building = building.selectedText;
                dto.FabArea = txtFabArea.Text.Trim();
                dto.Floor = floor.selectedText;
                dto.SubmainNo = txtSubmainNo.Text.Trim();
                dto.SysNo = SysNoBox.SelectedValue?.ToString();
                dto.TakeoffNo = txtTakeoffNo.Text.Trim();
                dto.X = numX.Value;
                dto.Y = numY.Value;
                dto.Z = numZ.Value;
                string jsons = JsonConvert.SerializeObject(dto);
                result = Client.Post(url + act, jsons);
            }
            if (Utils.ExceptionHandler(result) == null)
            {
                Utils.SysMsg("保存成功！");
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void FormHDCAdd_Load(object sender, EventArgs e)
        {
            //机台
            string getToolApi = string.Format("api/services/app/HKP_Tool/GetList?maxResultCount={0}", 99999);
            var result = Client.Get(getToolApi);
            ItemsDto<HKP_ToolDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_ToolDto>>(result);
            ToolIdBox.DataSource = res.result.items;
            ToolIdBox.DisplayMember = "toolName";
            ToolIdBox.ValueMember = "id";
            //系统
            string getSysApi = string.Format("api/services/app/HKP_System/GetList?maxResultCount={0}", 99999);
            var result1 = Client.Get(getSysApi);
            ItemsDto<HKP_SystemDto> dto = JsonConvert.DeserializeObject<ItemsDto<HKP_SystemDto>>(result1);
            SysNoBox.DataSource = dto.result.items;
            SysNoBox.DisplayMember = "sysName";
            SysNoBox.ValueMember = "sysNo";

            building.Init("Building", Client.RemoteHost, 0);
            building.selectedChangeEvent += new UserControlDropdown.SelectedChangeDelegate(BuildingSelectedChange);

            if (Item != null)
            {
                ToolIdBox.SelectedValue = Item.ToolId.ToString();
                txtBay.Text = Item.Bay;
                building.SetText(Item.Building);
                txtFabArea.Text = Item.FabArea;
                floor.SetText(Item.Floor);
                txtSubmainNo.Text = Item.SubmainNo;
                SysNoBox.SelectedValue = Item.SysNo;
                txtTakeoffNo.Text = Item.TakeoffNo;
                numX.Value = Item.X;
                numY.Value = Item.Y;
                numZ.Value = Item.Z;
            }
        }
        private void BuildingSelectedChange(object sender, EventArgs e)
        {
            floor.Init("Building", Client.RemoteHost, Convert.ToInt32(building.selectedValue));
        }

        private void DelToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除？", "系统", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                if (Item != null)
                {
                    string act = string.Format(url + "DelAndReleaseTakeOff?Id={0}", Item.Id);
                    string result = Client.Post(act, "");
                    if (Utils.ExceptionHandler(result) == null)
                    {
                        DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
            }
        }
    }
}
